<?php if ( ! defined("BASEPATH")) exit("No direct script access allowed");
class news_types extends MX_Controller
{
  public function __construct()
  {
    parent::__construct();
    $this->load->library("table");
    $this->load->helper(array("file", "form", "html"));
    $this->load->model("home_model");
    $this->load->model("news_types_model");
  }
  
  public function sort($field = "created", $order = "desc")
  {
    $this->news_types_model->dataSort($field, $order);
    $this->index();
  }

  public function index($target = 0)
  {
    $arr = $this->news_types_model->dataIndex($target); 
    $breakCrum = '<a href="'.base_url().'">'.t("Home").'</a> » <span>'.t("news_types").'</span>';
    $data = array("content" => $this->load->view("news_types/main", $arr ,true), "breakCrum" => $breakCrum);
    $this->load->view("templates/admin", $data);
  } 
  
  public function find()
  {
    if (!isset($_POST["txt_find"])) return redirect(site_url("news_types/index"));
    if (isset($_POST["btnReset"]) && $_POST["btnReset"] == "Reset") 
        $this->session->unset_userdata("sm_find_news_types");
    else {
        $keyword = $this->input->post("txt_find", true);
        if ($keyword != "") {
            notify_set_messages(notify_info(t("Result")." ".$keyword));
            $this->session->set_userdata("sm_find_news_types", $keyword);
        }
    }
   
    redirect(site_url("news_types/index"));
  }    
  public function add()
  {        
    $arr = $this->news_types_model->dataAdd();
    $breakCrum = '<a href="'.base_url().'">'.t("Home").'</a> » 
    <a href="/admin/news_types">'.t("news_types").'</a> » <span>Add</span>';
    $data = array("content" => $this->load->view("news_types/add", $arr, true), 
                  "breakCrum" => $breakCrum);
    $this->load->view("templates/admin", $data);
  }    
  
  public function edit($id)
{       
    $arr = $this->news_types_model->dataEdit($id);
    $breakCrum = '<a href="'.base_url().'">'.t("Home").'</a> » 
    <a href="/admin/news_types">'.t("news_types").'</a> » <span>Edit</span>';
    $data = array("content" => $this->load->view("news_types/edit", $arr, true),
            "breakCrum" => $breakCrum);
    $this->load->view("templates/admin", $data);
  }
  
  public function doAdd()
  {
    if(!$this->news_types_model->checkForm()) {
        notify_set_messages(notify_error(validation_errors()));
        return $this->add();
    }
    if ($this->news_types_model->doUpdate()) {
        notify_set_messages(notify_success(t("Record is added")));
        return redirect(site_url("news_types"));    
    }
      
  }
  
  public function doEdit()
  {        
    if(!$this->news_types_model->checkForm()) {
        notify_set_messages(notify_error(validation_errors()));
        return $this->edit(decrypt($_POST["idx"]));        
    }
    if ($this->news_types_model->doUpdate()) {
        notify_set_messages(notify_success(t("Record is updated")));
        redirect(site_url("news_types"));   
    }
     
  } 
  
  public function del($id)
  {
  
    if ($this->db->delete("news_types",array("id"=> $id)))
        notify_set_messages(notify_success(t("Record is deleted")));
    else
        notify_set_messages(notify_atten(t("Record not deleted")));    
    redirect(site_url("news_types"));    
  }        
                
}